jQuery(document).ready(function($) {
	"use strict";
	/* Menu Status*/
	jQuery( document ).on( 'click', '.edit-menu-item-megamenu-status', function() {
		var parent_li_item = jQuery( this ).parents( '.menu-item:eq( 0 )' );

		if( jQuery( this ).is( ':checked' ) ) {
			parent_li_item.addClass( 'crt-megamenu' );
			parent_li_item.find('.crt-tab-menu-items').show();
		} else 	{
			parent_li_item.removeClass( 'crt-megamenu' );
			parent_li_item.find('.crt-tab-menu-items').hide();
		}
	});
	
	var $settingswrap = jQuery( '#menu-to-edit' );
	
	//Show Icon Selection panel when icon is clicked
	$settingswrap.on( 'click' , '.crt-icon-selected' , function( e ){
		var icon_set = jQuery( this ).parents( '.crt-icon-wrapper' );
		icon_set.find( '.crt-icons' ).fadeToggle();
		icon_set.find( '.crt-icons-search' ).focus();
	});
	
	//change icon
	$settingswrap.on( 'click' , '.crt-icons .crt-icon-wrap' , function( e ){	
		var icon = jQuery( this ).find( '.crt-icon' );
		var icon_set = jQuery( this ).parents( '.crt-icon-wrapper' );
		icon_set.find( '.crt-icon-selected i' ).attr( 'class' , icon.attr( 'class' ) );
		icon_set.find( 'select' ).val( icon.data( 'crt-icon' ) ).change();
		jQuery( this ).parents( '.crt-icons' ).fadeOut();
	});
	
	//remove icon
	$settingswrap.on( 'click' , '.crt-icon-control .crt-remove-icon' , function( e ){
		var icon = jQuery( this ).find( '.crt-icon' );
		var icon_set = jQuery( this ).parents( '.crt-icon-wrapper' );
		icon_set.find( '.crt-icon-selected i' ).attr( 'class' , icon.attr( 'class' ) );
		icon_set.find( 'select' ).val( icon.data( 'crt-icon' ) ).change();
		jQuery( this ).parents( '.crt-icons' ).fadeOut();
	});
	
	/* Filter Icons */
	$settingswrap.on( 'keyup' , '.crt-icons-search' , function( e ){
		var icon_set = jQuery( this ).parents( '.crt-icon-wrapper' ).find( '.crt-icon-wrap' );
		var val = jQuery(this).val();
		if( val == '' ){
			icon_set.show();
		}
		else{
			icon_set.filter( ':not( [data-crt-search-terms*=' +val+ '] )' ).hide();
		}
	});
	
	/* Upload Background*/
	jQuery('.crt_upload_background_button').live('click',function() {  
		var current_add_ele = jQuery(this);
		var current_rmv_ele = jQuery(this).siblings('a.crt_clear_background_button');
		var preview = jQuery(this).siblings('span.edit-menu-item-background-wrapper');
		
		//thumbnail_value = jQuery(this).siblings('.edit-menu-item-thumbnail');  
		var thumbnail_id_value = jQuery(this).siblings('.edit-menu-item-background-id');  
		var nav_item_id = jQuery(this).attr('id');
		tb_show('', 'media-upload.php?type=image&post_id='+nav_item_id+'&TB_iframe=true&width=670&height=380');  
		window.send_to_editor = function(html) {  
			var _current_obj = jQuery('img',html);
			if( _current_obj.length <= 0 )
				_current_obj = jQuery(html);
			var imgurl = _current_obj.attr('src');  
			var classes = _current_obj.attr('class');  
			var id = classes.replace(/(.*?)wp-image-/, '');  
			thumbnail_id_value.val(id);
			jQuery.ajax({
				type: "POST",
				url : ajaxurl, 
				data: {
				   'action':'find_media_thumbnail',
				   'thumbnail_id':id
				}, 
				success : function(response){
					
					imgurl = response;
					preview.html('<img src="'+imgurl+'"  >'); 
					//thumbnail_value.val(imgurl);
					console.log(preview.html());
				},
				error: function(response){
					preview.html(''); 
					//thumbnail_value.val(imgurl);
				}
			});
			current_add_ele.hide();
			current_rmv_ele.show();
			tb_remove();  
		}  
		return false;  
	});
	
	jQuery('.crt_clear_background_button').live('click',function() {  
		var crt_rmv_ele = jQuery(this);
		var crt_add_ele = jQuery(this).siblings('a.crt_upload_background_button');
		var preview = jQuery(this).siblings('span.edit-menu-item-background-wrapper');
		//var thumbnail_value = jQuery(this).siblings('.edit-menu-item-thumbnail');  
		var thumbnail_id_value = jQuery(this).siblings('.edit-menu-item-background-id');  
		preview.html('');
		//thumbnail_value.val('');  
		thumbnail_id_value.val('');
		crt_add_ele.show();
		crt_rmv_ele.hide();
		return false;  
	}); 
		
});